package com.xinke.module.system.service.social;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.social.*;
import com.xinke.module.system.model.entity.social.SocialClientDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.social.SocialClientMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 社交客户端 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class SocialClientServiceImpl implements SocialClientService {

    @Resource
    private SocialClientMapper socialClientMapper;

    @Override
    public Long createSocialClient(SocialClientSaveReqVO createReqVO) {
        // 插入
        SocialClientDO socialClient = BeanUtils.toBean(createReqVO, SocialClientDO.class);
        socialClientMapper.insert(socialClient);
        // 返回
        return socialClient.getClientId();
    }

    @Override
    public void updateSocialClient(SocialClientSaveReqVO updateReqVO) {
        // 校验存在
        validateSocialClientExists(updateReqVO.getClientId());
        // 更新
        SocialClientDO updateObj = BeanUtils.toBean(updateReqVO, SocialClientDO.class);
        socialClientMapper.updateById(updateObj);
    }

    @Override
    public void deleteSocialClient(Long clientId) {
        // 校验存在
        validateSocialClientExists(clientId);
        // 删除
        socialClientMapper.deleteById(clientId);
    }

    private void validateSocialClientExists(Long clientId) {
        if (socialClientMapper.selectById(clientId) == null) {
            throw exception(null);
        }
    }

    @Override
    public SocialClientDO getSocialClient(Long clientId) {
        return socialClientMapper.selectById(clientId);
    }

    @Override
    public PageResult<SocialClientDO> getSocialClientPage(SocialClientPageReqVO pageReqVO) {
        return socialClientMapper.selectPage(pageReqVO);
    }

}